<?php
class captcha
{
	//变量依次为 图像对象，验证码字符数，基础字符串，随机后的字符串，验证码长度，验证码高度，验证码SESSION名称
	//是否加上干扰点，是否加上干扰线，干扰点个数，干扰线个数，字体，字体颜色，字号，字符编码(EN为英文，CH为中文)，左侧留白长度，字间距
	/*public function vcode()
	{
	    require_once(CP_PATH.'ext/code/Captcha.class.php');
		session_start();
		$vc=new captcha();
		$vc->CharSet="EN"; //CH为中文
		$vc->BaseString="a2bc3de4fgh5jkm6npqr7stu8ywx9yz"; //这里设置字符，中文150个字为最佳
		$vc->vcaptcha();
	}*/
	private $ImgObj;
	public $StringLength=4;
	public $BaseSting;
	public $RandSting;
	public $Width=70;
	public $Height=26;
	public $CaptchaSessionName='xfcode'; //Session 名字
	public $IsDrawPIxel=true;
	public $IsDrawLine=true;
	public $PixelNumber=10;	
	public $LineNumber=4;
	public $FontFace;
	public $FontColor;
	public $FontSize=16;
	public $CharSet;
	public $LeftSpeace=5;
	public $SpaceWidth=0;
	public function __construct()
	{
		if(!function_exists("gd_info"))
		{
		$this->error = 'GD库未启用或不正常！';
		exit();
		}
		else
		{
		$this->ImgObj = @imagecreate ($this->Width, $this->Height);
		}
	}
	//设定基础字串和启用SESSION
	public function setCaptchaStr()
	{
		if(!isset($_SESSION) && !ini_get('session.auto_start'))
		session_start();		
		if(empty($this->BaseString))
		$this->BaseString=strtoupper(md5(time()));
		$_SESSION[$this->CaptchaSessionName]=$this->RandSting;
		$BackgroundColor = imagecolorallocate ($this->ImgObj, rand(240,250), rand(250,240), rand(230,240)); //随机背景
	}
	//设置点状干扰
	public function setRandPixel()
	{
		for ($i=0;$i<$this->PixelNumber;$i++)
		{
		$PixelColor= imagecolorallocate ($this->ImgObj, rand(0,255), rand(0,255), rand(0,255));
		imagesetpixel($this->ImgObj,rand(0,$this->Width),rand(0,$this->Height),$PixelColor);
		}
	}
	//加入干扰线段
	public function setRandLine()
	{
		for ($i=0;$i<$this->LineNumber;$i++)
		{
		$LineColor = imagecolorallocate ($this->ImgObj, rand(0,255), rand(0,255), rand(0,255));
		imageline($this->ImgObj,rand(0,$this->Width),rand(0,$this->Height),rand(0,$this->Width),rand(0,$this->Height),$LineColor);
		}
	}
	//输出PNG图像
	public function vcaptcha()
	{
		$this->setCaptchaStr();
		$TotalLeter=strlen($this->BaseString);
		for ($i=0;$i<$this->StringLength;$i++)
		{
			if(count($this->FontColor)<3)
			$RandFontColor = imagecolorallocate ($this->ImgObj, rand(0,255), rand(0,128), rand(0,255));
			else
			$RandFontColor = imagecolorallocate ($this->ImgObj, $this->FontColor['0'], $this->FontColor['1'], $this->FontColor['2']);
			if($i==0)
			$FontX =$this->LeftSpeace;
			else
			$FontX+= $this->FontSize+$this->SpaceWidth;
			$SplitNum=($this->CharSet=="CH")?3:1;
			if($this->CharSet=="EN")
			{
			    $this->FontFace = dirname(__FILE__).'/elephant.ttf';//英文字体
				$TempChr=mb_strcut($this->BaseString, rand(0,$TotalLeter-3),1, 'utf-8');
				imagettftext($this->ImgObj,$this->FontSize,rand(-8,8),$FontX,rand(18,22),$RandFontColor,$this->FontFace,$TempChr);
				$this->RandSting.=$TempChr;
			}
			else if($this->CharSet=="CH")
			{
				$this->FontFace = dirname(__FILE__).'/htj.ttf'; //中文字体
				$this->SpaceWidth = 2;
				$this->FontSize = 14;
				$TempChr=mb_strcut($this->BaseString, rand(0,$TotalLeter-$SplitNum),$SplitNum, 'utf-8');
				imagettftext($this->ImgObj,$this->FontSize,rand(-8,8),$FontX,rand(18,22),$RandFontColor,$this->FontFace,$TempChr);
				$this->RandSting.=$TempChr;
			}
			else
			{
				$TempChr=mb_strcut($this->BaseString, rand(0,$TotalLeter-3),1, 'utf-8');
				imagestring($this->ImgObj, 9, $FontX, 2, $TempChr, $RandFontColor);
				$this->RandSting.=$TempChr;	
			}
			$_SESSION["$this->CaptchaSessionName"]=$this->RandSting;
		}
			if($this->IsDrawPIxel)
			$this->setRandPixel();
			if($this->IsDrawLine)
			$this->setRandLine();
			header ("Content-type: image/png");		
			imagepng ($this->ImgObj);
			imagedestroy ($this->ImgObj);
	}
}
?>